#!/bin/bash
#
# Copyright 2015-2017 Adrian DC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# === Git Remotes Projects ===
function gitrap()
{
  # Usage: gitrap (Development projects selector)

  # Variables
  local cnt=0;
  local key;
  local line_function;
  local line_usage;
  local list_functions=('');
  local project;

  # Check remote existence
  if ! git ls-remote --exit-code 'project' > /dev/null 2>&1; then

    # Detect roomservice.xml provided project
    if [ -f "$(gettop)/.repo/local_manifests/roomservice.xml" ]; then
      project=$(grep "Project: '" "$(gettop)/.repo/local_manifests/roomservice.xml" | sed "s/.* '\(.*\)' .*/\1/");
      if [ ! -z "${project}" ]; then

        # Extract project tag
        project=${project#* };
        project=${project% *};

        # Apply project target
        gitremoteset project "${project}";

        # Display selected project target
        echo '';
        echo -e " \e[1;33mgitrap: Default project remote set to \e[0m'${project}'";
        echo '';
        return;

      fi;
    fi;
  fi;

  # Display shtools* menu
  echo '';
  for line_function in $(typeset -F | grep gitrap | cut -d' ' -f 3 | grep -v '^gitrap$'); do

    # Parse usage
    line_usage=$(grep -a "Usage: ${line_function} " "${ANDROID_DEVELOPMENT_SHELL_TOOLS_DIR}/android_repo_projects.rc");
    if [ -z "${line_usage}" ]; then
      continue;
    fi;
    line_usage=${line_usage#*\(};
    line_usage=${line_usage%\)*};
    list_functions+=("${line_function}");

    # Item choice
    cnt=$((cnt + 1));
    echo -e "  \e[1;33m${cnt}: \e[1;37m${line_function} \e[1;32m[${line_usage}]\e[0m";

  done;
  echo '';

  # Request user input
  key=0;
  while [ -z "${key}" ] || [ ! -z "${key//[0-9]/}" ] || [ ${key} -le 0 ] || [ ${key} -gt ${cnt} ]; do
    echo -n " > Project to select [1-${cnt}] : ";
    read -r key;
  done;

  # Execute requested function
  echo '';
  ${list_functions[${key}]};
  echo '';
}

# === Git Remotes LineageOS (Sony 8996) Project ===
function gitraplineagesony8996()
{
  # Usage: gitraplineagesony8996 (LineageOS Sony 8996 development project remote)

  # LineageOS Sony 8996 development remote
  gitremoteset project https://github.com/AdrianDC/lineage_development_sony8996.git;
}

# === Git Remotes AOSP (Sony 8960) N Project ===
function gitrapaospsony8960n()
{
  # Usage: gitrapaospsony8960n (AOSP Sony 8960 Nougat development project remote)

  # AOSP 8960 Nougat development remote
  gitremoteset project https://github.com/AdrianDC/aosp_development_sony8960_n.git;
}

# === Git Remotes AOSP (Sony 8960) O Project ===
function gitrapaospsony8960o()
{
  # Usage: gitrapaospsony8960o (AOSP Sony 8960 O development project remote)

  # AOSP Sony 8960 O development remote
  gitremoteset project https://github.com/AdrianDC/aosp_development_sony8960_o.git;
}

# === Git Remotes AOSP (Sony 8960) Master Project ===
function gitrapaospsony8960master()
{
  # Usage: gitrapaospsony8960master (AOSP Sony 8960 Master development project remote)

  # AOSP Sony 8960 Master development remote
  gitremoteset project https://github.com/AdrianDC/aosp_development_sony8960_master.git;
}

# === Git Remotes AOSP (Sony SODP) Project ===
function gitrapaospsonysodp()
{
  # Usage: gitrapaospsonysodp (AOSP Sony SODP development project remote)

  # AOSP Sony SODP development remote
  gitremoteset project https://github.com/AdrianDC/aosp_development_sonysodp.git;
}

# === Git Remotes AOSP-CAF Project ===
function gitrapaospcaf()
{
  # Usage: gitrapaospcaf (AOSP-CAF development project remote)

  # AOSP-CAF development remote
  gitremoteset project https://github.com/AdrianDC/aosp_development_aospcaf.git;
}

# === Git Remotes LineageOS (Sony 8960) Master Project ===
function gitraplineagesony8960master()
{
  # Usage: gitraplineagesony8960master (LineageOS Sony 8960 Master development project remote)

  # LineageOS Sony 8960 Master development remote
  gitremoteset project https://github.com/AdrianDC/lineage_development_sony8960_master.git;
}

# === Git Remotes LineageOS (Sony SODP) Project ===
function gitraplineagesonysodp()
{
  # Usage: gitraplineagesonysodp (LineageOS Sony SODP development project remote)

  # LineageOS Sony SODP development remote
  gitremoteset project https://github.com/AdrianDC/lineage_development_sonysodp.git;
}

# === Git Remotes MultiROM (Sony) Project ===
function gitrapmultirom()
{
  # Usage: gitrapmultirom (MultiROM Sony development project remote)

  # MultiROM Sony development remote
  gitremoteset project https://github.com/AdrianDC/multirom_development_sony.git;
}

# === Git Remotes TWRP (Sony) Project ===
function gitraptwrp()
{
  # Usage: gitraptwrp (TWRP Sony development project remote)

  # TWRP Sony development remote
  gitremoteset project https://github.com/AdrianDC/twrp_development_sony.git;
}
